package com.battle.service.check;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import com.battle.service.config.EncryptConfig;
import cn.hutool.http.HttpUtil;
import com.google.gson.JsonObject;
import nirvana.core.exceptions.BizException;
import org.springframework.stereotype.Component;

/**
 * @author anni.zhu
 * @author anni.zhu@king-v.com
 * Created in 2024/11/25 14:23
 */
@Component
public class EncryptionUtil {

    /**
     * 调用远程加密
     *
     * @param value
     * @return
     */
    public Object encryption(Object value) {
        Object resultValue =value;

        String url=EncryptConfig.Url+EncryptConfig.EncryptUrl;
        JSONObject body=new JSONObject();
        body.put("key",EncryptConfig.Identification);
        body.put("algorithmType",EncryptConfig.AlgorithmType);
        body.put("encMode",EncryptConfig.EncMode);
        body.put("iv",EncryptConfig.Iv);
        body.put("padMode",EncryptConfig.PadMode);
        body.put("plainIsEncode",EncryptConfig.PlainIsEncode);
        body.put("data",value);

        String result = HttpUtil.createPost(url)
                .body(body.toString())
                .execute().body();

        //判断远程调用是否成功
        if (result!=null){
            JSONObject jsonObject = new JSONObject(result);
            String data = jsonObject.getStr("data");
            resultValue=data;
            //判断如果为空的话就报错提示加密失败
            if (data==null) throw new BizException("加密失败");
        }

        //数据完整性，进行加密
        String hmacUrl=EncryptConfig.Url+EncryptConfig.HmacEncryptUrl;
        JSONObject hmacBody=new JSONObject();
        hmacBody.put("key",EncryptConfig.HmacIdentfication);
        hmacBody.put("data",value);
        hmacBody.put("plainIsEncode",EncryptConfig.PlainIsEncode);
        String hmacResult = HttpUtil.createPost(url)
                .body(body.toString())
                .execute().body();
        //TODO 暂时不做操作，后续验收
        return resultValue;
    }

    /**
     * 调用远程进行解密
     *
     * @param value
     * @return
     */
    public Object decode(Object value) {
        Object resultValue = value;

        String url=EncryptConfig.Url+EncryptConfig.DecodeUrl;
        JSONObject body=new JSONObject();
        body.put("key",EncryptConfig.Identification);
        body.put("algorithmType",EncryptConfig.AlgorithmType);
        body.put("encMode",EncryptConfig.EncMode);
        body.put("iv",EncryptConfig.Iv);
        body.put("padMode",EncryptConfig.PadMode);
        body.put("plainIsEncode",EncryptConfig.PlainIsEncode);
        body.put("encryptData",value);

        String result = HttpUtil.createPost(url)
                .body(body.toString())
                .execute().body();

        //判断远程调用是否成功
        if (result!=null){
            JSONObject jsonObject = new JSONObject(result);
            String data = jsonObject.getStr("data");
            resultValue=data;
            //判断如果为空的话就报错提示解密失败
            if (data==null) throw new BizException("解密失败");
        }

        return resultValue;
    }


    /**
     * hmac远程加密，传入key和value
     * @param value
     * @param
     * @return
     */
    public Object encryptionByHmac(Object value) {
        Object resultValue =value;
//        //数据完整性，进行hmac加密
        String hmacUrl=EncryptConfig.Url+EncryptConfig.HmacEncryptUrl;
        JSONObject hmacBody=new JSONObject();
        hmacBody.put("key",EncryptConfig.HmacIdentfication);
        hmacBody.put("data",value);
        hmacBody.put("plainIsEncode",EncryptConfig.PlainIsEncode);
        String hmacResult = HttpUtil.createPost(hmacUrl)
                .body(hmacBody.toString())
                .execute().body();

        //判断远程调用是否成功
        if (hmacResult!=null){
            JSONObject jsonObject = new JSONObject(hmacResult);
            String data = jsonObject.getStr("data");
            resultValue=data;
            //判断如果为空的话就报错提示加密失败
            if (data==null) throw new BizException("加密失败");
        }
        return resultValue;
    }

}
